package com.ishare.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ishare.admin.mapper.SysMenuMapper;
import com.ishare.admin.service.SysMenuService;
import com.ishare.admin.service.SysRoleMenuService;
import com.ishare.common.entity.po.SysMenu;
import com.ishare.common.entity.vo.MenuTreeDataVO;
import com.ishare.common.entity.vo.MenuVO;
import com.ishare.common.utils.MapUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenu> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menuIdList.contains(menu.getId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<SysMenu> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    @Override
    public List<MenuVO> getUserMenuList(Long userId) {
        List<MenuVO> menuVOS = null;
        if (userId == null){
            menuVOS = this.queryUserAllMenu(null);
        }else {
            menuVOS = this.queryUserAllMenu(userId);
        }
        List<MenuVO> root = new ArrayList<>();
        MenuVO menuVO = new MenuVO();
        menuVO.setRouter("root");
        menuVO.setChildren(menuVOS);
        root.add(menuVO);
        return root;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long menuId) {
        //删除菜单
        this.removeById(menuId);
        //删除菜单与角色关联
        sysRoleMenuService.removeByMap(new MapUtils().put("menu_id", menuId));
    }

    @Override
    public List<MenuVO> queryUserAllMenu(Long userId) {
        return this.transformTree(userId);
    }

    //转换树形菜单
    private List<MenuVO> transformTree(Long userId){
        List<MenuVO> root = new ArrayList<>();
        List<SysMenu> sysMenus;
        if (userId != null){
            sysMenus = baseMapper.queryUserAllMenu(userId);
        }else {
            sysMenus = this.selectList();
        }
        List<MenuVO> children = new ArrayList<>();
        sysMenus.forEach(menu ->{
            if (menu.getPid()==0){
                //主菜单
                MenuVO menuVO = new MenuVO();
                BeanUtils.copyProperties(menu,menuVO);
                root.add(menuVO);
            }else {
                //子菜单
                MenuVO menuVO = new MenuVO();
                BeanUtils.copyProperties(menu,menuVO);
                children.add(menuVO);
            }
        });
        root.forEach(current ->{
            List<MenuVO> subList = new ArrayList<>();
            children.forEach(child ->{
                //主菜单添加子菜单
                if (child.getPid().equals(current.getId())){
                    //上级菜单
                    child.setSupMenu(current.getName());
                    subList.add(child);
                }
            });
            current.setChildren(subList);
        });
        return root;
    }

    @Override
    public List<SysMenu> selectList() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<MenuTreeDataVO> select(Long userId) {
        List<MenuVO> children = this.queryUserAllMenu(userId);
        List<MenuTreeDataVO> sup = new ArrayList<>();
        children.forEach(menuVO -> {
            MenuTreeDataVO menu = new MenuTreeDataVO();
            menu.setTitle(menuVO.getName());
            menu.setMenuId(menuVO.getId());
            menu.setValue(menuVO.getId());
            menu.setKey(menuVO.getId());
            //转换子菜单
            List<MenuVO> subMenu = menuVO.getChildren();
            List<MenuTreeDataVO> sub = new ArrayList<>();
            for (MenuVO subMenuVO : subMenu) {
                MenuTreeDataVO entity = new MenuTreeDataVO();
                entity.setTitle(subMenuVO.getName());
                entity.setValue(subMenuVO.getId());
                entity.setMenuId(subMenuVO.getId());
                entity.setKey(subMenuVO.getId());
                sub.add(entity);
                menu.setChildren(sub);
            }
            sup.add(menu);
        });
        List<MenuTreeDataVO> root = new ArrayList<>();
        //拼接根菜单
        MenuTreeDataVO menu = new MenuTreeDataVO();
        menu.setMenuId(0L);
        menu.setValue(0L);
        menu.setKey(0L);
        menu.setTitle("一级菜单");
        menu.setChildren(sup);
        root.add(menu);
        return root;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));
    }
}
